<template>
    <div class="app-container app-content">
      <el-row>
        <el-col :span="24">
          <el-row class="line-bottom" v-show="showSearch">
            <el-col class="form_col">
              <el-form :model="queryParams" ref="queryForm" :size="size" v-show="showSearch" :label-width="labelWidth">
                <div id="searchFilter" :gutter="10" style="display: flex; flex-wrap: wrap">
                  <!-- <el-form-item label="字典排序" prop="dictSort">
                    <el-input
                      v-model="queryParams.dictSort"
                      placeholder="请输入字典排序"
                      clearable
                      @keyup.enter.native="handleQuery"
                    />
                  </el-form-item> -->
                  <el-form-item label="字典标签" prop="dictLabel">
                    <el-input
                      v-model="queryParams.dictLabel"
                      placeholder="请输入字典标签"
                      clearable
                      @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="字典名称" prop="dictType">
                    <el-select filterable v-model="queryParams.dictType" placeholder="请选择字典名称">
                      <el-option
                        v-for="(item, index) in dictTypeOption"
                        :key="index"
                        :label="item.dictName"
                        :value="item.dictType"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                  <!-- <el-form-item label="字典键值" prop="dictValue">
                    <el-input
                      v-model="queryParams.dictValue"
                      placeholder="请输入字典键值"
                      clearable
                      @keyup.enter.native="handleQuery"
                    />
                  </el-form-item> -->
                  <!-- <el-form-item label="样式属性" prop="cssClass">
                    <el-input
                      v-model="queryParams.cssClass"
                      placeholder="请输入样式属性"
                      clearable
                      @keyup.enter.native="handleQuery"
                    />
                  </el-form-item> -->
                  <!-- <el-form-item label="表格回显样式" prop="listClass">
                    <el-input
                      v-model="queryParams.listClass"
                      placeholder="请输入表格回显样式"
                      clearable
                      @keyup.enter.native="handleQuery"
                    />
                  </el-form-item> -->
                  <el-form-item label="是否默认" prop="isDefault">
                    <el-select v-model="queryParams.isDefault" placeholder="请选择是否默认" style="width: 100%;">
                      <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value">{{dict.label}}</el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item>
                  </el-form-item>
                </div>
              </el-form>
            </el-col>
            <el-col class="form_query">
              <el-button v-show="collapseAble" type="text" @click="shiftCollapseAble" class="text">
                <span>
                  {{ fold ? '收起筛选' : '更多筛选' }}
                </span>
              </el-button>
              <el-button type="primary" @click="handleQuery" size="mini">查询</el-button>
              <el-button @click="resetQuery" size="mini">重置</el-button>
            </el-col>
          </el-row>
          <el-row :gutter="10" class="mb8 padding_top_bottom">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['system:data:add']"
              >新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['system:data:edit']"
              >修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['system:data:remove']"
              >批量删除</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                v-hasPermi="['system:data:export']"
              >导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" :height="getAvailableHeight" ref="table">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="字典编码" align="center" prop="dictCode" :show-overflow-tooltip="true"/>
            <el-table-column label="字典排序" align="center" prop="dictSort" :show-overflow-tooltip="true"/>
            <el-table-column label="字典标签" align="center" prop="dictLabel" :show-overflow-tooltip="true"/>
            <el-table-column label="字典键值" align="center" prop="dictValue" :show-overflow-tooltip="true"/>
            <el-table-column label="字典名称" align="center" prop="dictType" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <span type="text" v-for="(item, index) in dictTypeOption" v-if="scope.row.dictType == item.dictType" >{{item.dictName}}</span>
              </template>
            </el-table-column>
            <el-table-column label="样式属性" align="center" prop="cssClass" :show-overflow-tooltip="true"/>
            <el-table-column label="表格回显样式" align="center" prop="listClass" :show-overflow-tooltip="true"/>
            <el-table-column label="是否默认" align="center" prop="isDefault" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isDefault"/>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.status"
                  active-value="0"
                  inactive-value="1"
                  @change="handleStatusChange(scope.row)"
                ></el-switch>
              </template>
            </el-table-column>
            <!-- <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/> -->
            <el-table-column label="操作" align="center"  width="120px" fixed="right">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:data:edit']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['system:data:remove']"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-row>

      <!-- 添加或修改字典数据对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth" :inline="true">
          <el-form-item label="字典排序" prop="dictSort">
            <!-- <el-input v-model="form.dictSort" placeholder="请输入字典排序" /> -->
            <el-input-number v-model="form.dictSort" :min="0" :max="10" label="请输入字典排序"></el-input-number>
          </el-form-item>
          <el-form-item label="字典标签" prop="dictLabel">
            <el-input v-model="form.dictLabel" placeholder="请输入字典标签" />
          </el-form-item>
          <el-form-item label="字典名称" prop="dictType">
            <el-select filterable v-model="form.dictType" placeholder="请选择字典名称" style="width: 202px;">
              <el-option
                v-for="(item, index) in dictTypeOption"
                :key="index"
                :label="item.dictName"
                :value="item.dictType"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="字典键值" prop="dictValue">
            <el-input v-model="form.dictValue" placeholder="请输入字典键值" />
          </el-form-item>
          <el-form-item label="样式属性" prop="cssClass">
            <el-input v-model="form.cssClass" placeholder="请输入样式属性" />
          </el-form-item>
          <el-form-item label="表格回显样式" prop="listClass">
            <el-input v-model="form.listClass" placeholder="请输入表格回显样式" />
          </el-form-item>
          <el-form-item label="是否默认" prop="isDefault">
            <el-select v-model="form.isDefault" placeholder="请选择是否默认" style="width: 202px;">
              <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value">{{dict.label}}</el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" style="width: 202px;"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>

  <script>
  import { listData, getData, delData, addData, updateData, getDictType, updateStatus } from "@/api/basedata/dictdata";

  export default {
    name: "DictData",
    dicts: ['sys_yes_no'],
    data() {
      return {
        size:'small',
        collapseAble: false,
        fold: false,
        maxShow: '4',
        labelWidth: '100px',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 字典数据表格数据
        dataList: [],
        // 数据字典类型
        dictTypeOption: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,
          dictSort: null,
          dictLabel: null,
          dictValue: null,
          dictType: null,
          cssClass: null,
          listClass: null,
          isDefault: null,
          status: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          // dictCode: [
          //   { required: true, message: "字典编码不能为空", trigger: "blur" }
          // ],
          dictLabel: [
            { required: true, message: "字典标签不能为空", trigger: "blur" }
          ],
          dictType: [
            { required: true, message: "字典类型不能为空", trigger: "change" }
          ],
          dictValue: [
            { required: true, message: "字典键值不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getDictTypeList()
    },
    mounted() {
      // 通过最大显示个数控制展开/折叠
      if (this.maxShow > 0) {
        this.minShowControl()
      }
    },
    computed: {
      getAvailableHeight() {
        return this.screenHeight - (this.showSearch ? 252 : 200)
      },
    },
    methods: {
      // 状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      let form = {dictCode: row.dictCode, status: row.status}
      this.$modal.confirm('确认要"' + text + '""' + row.dictLabel + '"字典数据吗？').then(function() {
      return updateStatus(form)
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        this.getList()
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
      // 获取数据字典类型
      getDictTypeList(){
        getDictType().then(response => {
          this.dictTypeOption = response.data;
        });
      },
      // 展开折叠按钮
      shiftCollapseAble() {
        this.fold = !this.fold
        this.minShowControl()
      },
      // 通过maxShow控制元素显示/折叠
      minShowControl() {
        const group = window.document.querySelectorAll(`#searchFilter .el-form-item.el-form-item--${this.size}`)
        const len = group?.length ? group?.length - 1 : 0
        if (this.maxShow < len) {
          group.forEach((item, index) => {
            if (index > this.maxShow - 1 && index < len) {
              item.hidden = !this.fold
            }
          })
          this.collapseAble = true
        } else {
          this.collapseAble = false
        }
      },
      /** 查询字典数据列表 */
      getList() {
        this.loading = true;
        listData(this.queryParams).then(response => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          dictCode: null,
          dictSort: null,
          dictLabel: null,
          dictValue: null,
          dictType: null,
          cssClass: null,
          listClass: null,
          isDefault: 'Y',
          status: '',
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.dictCode)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加字典数据";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const dictCode = row.dictCode || this.ids
        getData(dictCode).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改字典数据";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.dictCode != null) {
              updateData(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addData(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const dictCodes = row.dictCode || this.ids;
        this.$modal.confirm('是否确认删除字典数据编号为"' + dictCodes + '"的数据项？').then(function() {
          return delData(dictCodes);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('/system/dict/data/export', {
          ...this.queryParams
        }, `data_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  <style lang="scss" scoped>
  .form_col{
    width: 80%;
    ::v-deep .el-form {
      width: 100%;
    }
    ::v-deep .el-form-item {
      width: 25%;
    }
  }
  .form_query{
    width: 20%;
    text-align: right;
  }
  </style>
